package com.example.tomcatdemo.control;

import com.example.tomcatdemo.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/user_delete.action")
public class UserDeleteControl extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idStr = request.getParameter("id");
        if (idStr != null && !idStr.isEmpty()) {
            int id = Integer.parseInt(idStr);
            UserService service = UserService.getInstance();
            int rows = service.deleteUserById(id);

            if (rows>0){
                response.sendRedirect("./user_list.action");
            }else {
                request.setAttribute("DelErr","数据添加失败，请联系开发商！");
                request.getRequestDispatcher("./user_list.jsp").forward(request,response);
            }
        }
    }
}
